<?php

namespace App\Entity\Banner;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Base\Annotation\Common;
use App\Repository\Banner\BannerPositionRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass=BannerPositionRepository::class)
 */
class BannerPosition
{
    /**
     * @Common(cnName="#",listType="text",editType="text")
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"banner"})
     */
    private $id;

    /**
     * @Common(cnName="板块名称",listType="text",editType="text",width="")
     * @ORM\Column(type="string", length=255)
     * @Groups({"banner"})
     */
    private $name;

    /**
     * @Common(cnName="排序",listType="text",editType="text")
     * @ORM\Column(type="integer", nullable=true)
     * @Groups({"banner"})
     */
    private $sorts;

    /**
     * @ORM\OneToMany(targetEntity=Banners::class, mappedBy="position")
     * @ORM\OrderBy({"sorts"="ASC"})
     * @Groups({"banners"})
     */
    private $banners;

    public function __construct()
    {
        $this->banners = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getSorts(): ?int
    {
        return $this->sorts;
    }

    public function setSorts(?int $sorts): self
    {
        $this->sorts = $sorts;

        return $this;
    }

    /**
     * @return Collection|Banners[]
     */
    public function getBanners(): Collection
    {
        return $this->banners;
    }

    public function addBanner(Banners $banner): self
    {
        if (!$this->banners->contains($banner)) {
            $this->banners[] = $banner;
            $banner->setPosition($this);
        }

        return $this;
    }

    public function removeBanner(Banners $banner): self
    {
        if ($this->banners->contains($banner)) {
            $this->banners->removeElement($banner);
            // set the owning side to null (unless already changed)
            if ($banner->getPosition() === $this) {
                $banner->setPosition(null);
            }
        }

        return $this;
    }
}
